package cn.hh.org.service.Impl;

import cn.hh.basic.exception.BusinessException;
import cn.hh.basic.service.impl.BaseServiceImpl;
import cn.hh.basic.util.BaiduAuditUtils;
import cn.hh.org.domain.Employee;
import cn.hh.org.domain.Shop;
import cn.hh.org.domain.ShopAuditLog;
import cn.hh.org.mapper.EmployeeMapper;
import cn.hh.org.mapper.ShopAuditLogMapper;
import cn.hh.org.mapper.ShopMapper;
import cn.hh.org.service.ShopService;
import cn.hh.org.vo.ShopEchartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;


/**
 * 业务实现类：员工
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        //一：校验
        //1.空值校验 - 试一下
        if (StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(shop.getAddress()) || StringUtils.isEmpty(shop.getEmployee().getUsername())
                || StringUtils.isEmpty(shop.getEmployee().getEmail()) || StringUtils.isEmpty(shop.getEmployee().getPhone())
                || StringUtils.isEmpty(shop.getEmployee().getPassword())) {
            throw new BusinessException("数据不能为空");
        }
        //2.密码是否一致：不做，如果要做在t_employee加一个字段comfirmPassword即可

        //3.是否曾经入驻过
        Shop shopTmp = shopMapper.loadByMessage(shop);
        if (shopTmp != null) {
            throw new BusinessException("已经入驻，请直接登录。忘记密码请联系管理员!!!");
        }

        // 4.调用百度AI智能审核文本工具类,审核用户的店铺名称
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("请规范输入店铺名称!");
        }

        //二：添加到数据库
        //1.添加t_employee
        Employee employee = shop.getEmployee();
        employeeMapper.save(employee);

        //2.添加t_shop
        shop.setAdmin_id(employee.getId());
        shopMapper.save(shop);

        //3.修改t_employee的shop_id
        employee.setShop_id(shop.getId());
        employeeMapper.update(employee);
    }

    /**
     * 驳回店铺
     *
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        // 1.修改店铺的状态为通过

        // 2.保存日志信息
        shopAuditLog.setAuditId(325L); // usacqtrybszwbaje
        shopAuditLogMapper.save(shopAuditLog);
        try {
            //1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            helper.setFrom("744430983@qq.com");
            helper.setSubject("宠物之家店铺入驻结果通知");
            //3.设置文本内容,第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>您的店铺入驻申请信息我平台已审核,但是内容不合格被驳回,请修改店铺信息后,再次申请</h1>" +
                    "店铺信息修改链接:<a href='http://localhost:8081/#/Register2?shopId=" + shopAuditLog.getShopId() + "'>点我修改店铺信息</a>", true);
            //4.收件人
            helper.setTo("1965539956@qq.com");
            //5.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 通过店铺
     *
     * @param shopAuditLog
     */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        // 1.修改店铺的状态为驳回

        // 2.保存日志信息
        shopAuditLog.setAuditId(325L);
        shopAuditLogMapper.save(shopAuditLog);
        // 3.发送修改店铺信息邮件给用户
        try {
            //1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            helper.setFrom("744430983@qq.com");
            helper.setSubject("宠物之家店铺入驻结果通知");
            //3.设置文本内容,第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>您的店铺入驻申请信息我平台已审核通过,请点击下方链接激活店铺</h1>" +
                    "店铺信息激活链接:<a href='http://localhost:8080/shop/" + shopAuditLog.getShopId() + "'>点我激活店铺</a>", true);
            //4.收件人
            helper.setTo("1965539956@qq.com");
            //5.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 查询店铺报表数据
     *
     * @return
     */
    @Override
    public List<ShopEchartVo> getShopState() {
        return shopMapper.getShopState();
    }
}
